<template>
  <div class="container">
    <a-space direction="vertical" style="width: 100%" :size="16">
      <a-card style="width: 100%" title="商品管理">
        <a-form :model="formState">
          <a-row>
            <a-col :span="4">
              <a-form-item label="商品名称">
                <a-input
                  style="width: 80%"
                  v-model:value="formState.brandName"
                  placeholder="输入商品名称关键词"
                />
              </a-form-item>
            </a-col>

            <a-col :span="4">
              <a-form-item label="商品编号">
                <a-input
                  style="width: 80%"
                  v-model:value="formState.number"
                  placeholder="输入商品编号"
                />
              </a-form-item>
            </a-col>

            <a-col :span="4">
              <a-form-item label="所属品牌">
                <a-input
                  style="width: 80%"
                  v-model:value="formState.searchCondition"
                  placeholder="输入商品品牌"
                />
              </a-form-item>
            </a-col>

            <a-col :span="4">
              <a-form-item label="商品类目">
                <a-select
                  ref="select"
                  v-model:value="formState.typeId"
                  style="width: 80%"
                  placeholder="商品类目"
                  @change="handleChangeType"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option
                    v-for="item in goodsTypeList"
                    :key="item.id"
                    :value="item.id"
                    >{{ item.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="子类目">
                <a-select
                  ref="select"
                  v-model:value="formState.subTypeId"
                  style="width: 80%"
                  placeholder="商品子类目"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <template
                    v-if="
                      subcategoryList[0] &&
                      subcategoryList[0].childrenList.length > 0
                    "
                  >
                    <a-select-option
                      v-for="item in subcategoryList[0].childrenList"
                      :key="item.id"
                      :value="item.id"
                      >{{ item.name }}</a-select-option
                    >
                  </template>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="成色">
                <a-select
                  ref="select"
                  v-model:value="formState.finenessId"
                  style="width: 80%"
                  placeholder="商品成色"
                >
                  <a-select-option :value="null">全部</a-select-option>
                  <a-select-option
                    v-for="item in goodsFinenessList"
                    :key="item.id"
                    :value="item.id"
                    >{{ item.content }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :span="4">
              <a-form-item label="商品状态">
                <a-select
                  ref="select"
                  v-model:value="formState.status"
                  style="width: 80%"
                  placeholder="商品状态"
                >
                  <a-select-option :value="-1">全部</a-select-option>
                  <a-select-option
                    :value="item.value"
                    v-for="(item, index) in goodsStateEnum.data"
                    >{{ item.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <div class="layout-flexSpace">
          <a-space wrap>
            <a-button type="primary" @click="handleSearch">查询</a-button>
            <a-button @click="handleReset">重置</a-button>
          </a-space>
          <a-space wrap>
            <a-button type="primary" @click="handleExport">导出</a-button>
          </a-space>
        </div>
      </a-card>

      <a-card>
        <a-table
          :dataSource="dataSource"
          :columns="goodsColumns"
          :pagination="pagination"
          @change="handleChangeTable"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'name'">
              <div style="display: flex; align-items: center">
                <a-image
                  :src="record.coverPic"
                  :preview="{
                    src: record.coverPic,
                  }"
                  :width="60"
                />
                <div style="margin-left: 20px">
                  <div>{{ record.name }}</div>
                  <div>商品编号：{{ record.sku }}</div>
                </div>
              </div>
            </template>

            <template v-if="column.key === 'shopPrice'">
              ￥{{ record.shopPrice }}
            </template>
            <template v-if="column.key === 'isSale'">
              {{
                record.isSale == 1
                  ? "已售出"
                  : record.isShelf == 1
                  ? "已上架"
                  : "未上架"
              }}
            </template>
            <template v-if="column.key === 'operate'">
              <a-space wrap>
                <a-button type="primary" @click="seeFunc(record.id)"
                  >查看</a-button
                >
                <a-popconfirm
                  title="是否确定要下架该商品?"
                  ok-text="是"
                  cancel-text="否"
                  v-if="record.isShelf == 1 && record.isSale == 0"
                  @confirm="handleSell(record.id, 1)"
                >
                  <a-button>下架</a-button>
                </a-popconfirm>
                <template v-if="record.isSale == 0">
                  <a-button type="primary" @click="handleEdit(record.id)"
                    >编辑</a-button
                  >
                  <a-popconfirm
                    title="是否确定要上架该商品?"
                    ok-text="是"
                    cancel-text="否"
                    v-if="record.isShelf == 0"
                    @confirm="handleSell(record.id, 2)"
                  >
                    <a-button>上架</a-button>
                  </a-popconfirm>
                  <a-popconfirm
                    title="是否要删除该数据?"
                    ok-text="是"
                    cancel-text="否"
                    @confirm="confirm(record.id)"
                  >
                    <a-button type="primary" danger>删除</a-button>
                  </a-popconfirm>
                </template>
              </a-space>
            </template>
          </template>
        </a-table>
      </a-card>
    </a-space>

    <!-- 商品详情 -->
    <see ref="seeRef"></see>
    <add ref="addRef"></add>
  </div>
</template>
<script setup>
import { reactive, ref, onMounted } from "vue";
import { message } from "ant-design-vue";
import { goodsColumns } from "./columns";
import goodsStateEnum from "@/enums/goodsStateEnum";
import see from "./goodsManageModule/see.vue";
import add from "./goodsManageModule/add.vue";
import { goodsType, goodsFineness } from "@/api/order";
import { exporeFile } from "@/utils/exporeFile";
import {
  goodsList,
  exportGoods,
  deleteGoods,
  editStatus,
} from "@/api/goods";

const pagination = ref({
  current: 1,
  pageSize: 10,
  total: 0,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total) => `共 ${total} 条`,
});
const formState = ref({
  page: pagination.value.current,
  pageSize: pagination.value.pageSize,
  brandId: undefined,
  brandName: undefined,
  finenessId: undefined,
  number: undefined,
  status: undefined,
  subTypeId: undefined,
  typeId: undefined,
  searchCondition: undefined,
});
const dataSource = ref([]);

//查看详情
const seeRef = ref(null);
const seeFunc = (id) => {
  seeRef.value.seeFunc(id);
};

// 上下架
const handleSell = async (id, status) => {
  const result = await editStatus({ id, status });
  if (result.code == 200) {
    getGoodsList();
  }
};
const handleExport = async () => {
  const data = await exportGoods(formState.value);
  exporeFile("商品管理列表.xls", data);
};
const addRef = ref(null);
const handleEdit = (id) => {
  addRef.value.addFunc(id, "edit");
};
function handleSearch() {
  formState.value.page = 1
  formState.value.pageSize = 10
  getGoodsList();
}
function handleReset() {
  formState.value = {
    page: 1,
    pageSize: 10,
    brandId: undefined,
    brandName: undefined,
    finenessId: undefined,
    number: undefined,
    status: undefined,
    subTypeId: undefined,
    typeId: undefined,
    searchCondition: undefined,
  };
  pagination.value.current = 1
  pagination.value.pageSize = 10
  getGoodsList();
}

const confirm = async (id) => {
  const result = await deleteGoods({ id: id });
  if (result.code == 200) {
    message.info("删除成功");
    getGoodsList();
  }
};

const goodsTypeList = ref([]);
const getGoodsType = async () => {
  const result = await goodsType();
  if (result.code == 200) {
    goodsTypeList.value = result.data;
  }
};
const goodsFinenessList = ref([]);
const getGoodsFineness = async () => {
  const result = await goodsFineness();
  if (result.code == 200) {
    goodsFinenessList.value = result.data;
  }
};
const subcategoryList = ref([]);
function handleChangeType(val) {
  formState.value.subTypeId = undefined;
  subcategoryList.value = goodsTypeList.value.filter(
    (value) => value.id == val
  );
}
const getGoodsList = async () => {
  const result = await goodsList(formState.value);
  if (result.code == 200) {
    dataSource.value = result.data.list;
    pagination.value.total = result.data.total;
  }
};
// 分页
function handleChangeTable(page) {
  pagination.value = page;
  formState.value.page = page.current;
  formState.value.pageSize = page.pageSize;
  getGoodsList();
}
onMounted(() => {
  getGoodsType();
  getGoodsFineness();
  getGoodsList();
});
</script>

<style lang="less"></style>
